<?php
include_once('data.php');
include_once(Utils::getPath(array('model', 'message.php')));

class MessageRepository
{
	private $lastFilter;
	
	public function getById($messageId)
	{
		if (is_numeric($messageId))
		{
			$row = Database::select('*', 'messages', array(array('Id = ?', $messageId, PDO::PARAM_INT)));
			
			if ($row->rowCount() == 1)
			{
				$message = $row->fetchObject('Message');
				return $message;
			}
		}
		return null;
	}
	
	public function getAll($order = null, $desc = null, $pageIndex = 0, $pageCount = 10)
	{
		return $this->find(null, $order, $desc, $pageIndex, $pageCount);
	}
	
	public function findByUsername($userId, $read = null, $order = null, $desc = null, $pageIndex = 0, $pageCount = 10)
	{
		$filterParams = array();
		$filterParams[] = array("IdSender = ? OR", $userId, PDO::PARAM_INT);
		$filterParams[] = array("IdReceiver = ?", $userId, PDO::PARAM_INT);
		
		if(!is_null($read))
			$filterParams[] = array("AND IsRead = ?", (int)$read, PDO::PARAM_BOOL);
		
		return $this->find($filterParams, $order, $desc, $pageIndex, $pageCount);
	}
	
	public function findByReceiver($receiverId, $read = null, $order = null, $desc = null, $pageIndex = 0, $pageCount = 10)
	{
		$filterParams = array();
		$filterParams[] = array("IdReceiver = ?", $receiverId, PDO::PARAM_INT);
		
		if(!is_null($read))
			$filterParams[] = array("AND IsRead = ?", (int)$read, PDO::PARAM_BOOL);
		
		return $this->find($filterParams, $order, $desc, $pageIndex, $pageCount);
	}
	
	public function findBySender($senderId, $read = null, $order = null, $desc = null, $pageIndex = 0, $pageCount = 10)
	{
		$filterParams = array();
		$filterParams[] = array("IdSender = ?", $senderId, PDO::PARAM_INT);
		
		if(!is_null($read))
			$filterParams[] = array("AND IsRead = ?", (int)$read, PDO::PARAM_BOOL);
		
		return $this->find($filterParams, $order, $desc, $pageIndex, $pageCount);
	}
	
	public function find($filterParams, $order = null, $desc = null, $pageIndex = 0, $pageCount = 10)
	{
		$rows = Database::select('*', 'messages', $filterParams, $order, $desc, $pageIndex * $pageCount, $pageCount);
		$this->lastFilter = $filterParams;
		
		if ($rows->rowCount() > 0)
		{
			$messages = $rows->fetchAll(PDO::FETCH_CLASS, "Message");
			return $messages;
		}
		return null;
	}
	
	public function countLast()
	{
		$rows = Database::select('Count(*)', 'messages', $this->lastFilter, null, null, null, null);
		
		if ($rows->rowCount() == 1)
		{
			$row = $rows->fetch(PDO::FETCH_NUM);
			return $row[0];
		}
		return null;
	}
	
	private function getLast()
	{
		$row = Database::select('*', 'messages', null, 'Id', true, 0, 1);

		if ($row->rowCount() == 1)
		{
			$message = $row->fetchObject('Message');
			return $message;
		}
		return null;
	}
	
	public function persist($message)
	{
		$values = array();

		$values[] = array('IdSender', $message->getIdSender(), PDO::PARAM_INT);
		$values[] = array('IdReceiver', $message->getIdReceiver(), PDO::PARAM_INT);
		$values[] = array('Subject', $message->getSubject(), PDO::PARAM_STR);
		$values[] = array('Text', $message->getText(), PDO::PARAM_STR);
		$values[] = array('Date', $message->getDate(), PDO::PARAM_STR);
		
		if (!$message->IsPersited())
		{	
			$res = Database::insert('messages', $values);
			if ($res)
				return $this->getLast();
		}
		else
		{
			$values[] = array('IsRead', (int)$message->getIsRead(), PDO::PARAM_BOOL);
			
			$res = DataBase::update('messages', $values, array(array('Id = ?', $message->getId(), PDO::PARAM_INT)));			
			if ($res)
				return $this->getById($message->getId());
		}
		
		return null;
	}
	
	public function remove($message)
	{
		$res = Database::remove('messages', array(array('Id = ?', $message->getId(), PDO::PARAM_INT)));
		return $res;
	}
}

?>